package backend.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import backend.attribute.manager.AttributeSetManager;
import backend.attribute.manager.AttributeValueManager;
import backend.product.manager.ProductAttributeDropdownValueManager;
import backend.product.manager.ProductAttributeSetManager;
import backend.product.manager.ProductManager;
import com.opensymphony.xwork2.ActionSupport;
import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;
import core.attribute.model.AttributeValue;
import core.product.model.Product;
import core.product.model.ProductAttributeDropdownValue;
import core.product.model.ProductAv;

public class ProductAttributeSetController  extends ActionSupport implements ServletRequestAware
{
	private static final long serialVersionUID = 1L;
	private int p;
	private ProductAttributeSetManager pasm ;
	private AttributeSetManager asm ;
	private ProductManager pm ;
	private ProductAttributeDropdownValueManager pavdm ;
	private Long productUuid ;
	private Product product ;
	private List<AttributeSet> attributesets ;
	private List<AttributeSet> assignedAttributesets ;
	private Long attributeSetUuId ;
	private Map <AttributeSet,List<ProductAv>> attributeSetToAv ;
	private HttpServletRequest request;
	
	public ProductAttributeSetController()
	{
		pasm = new ProductAttributeSetManager();
		pm = new ProductManager();
		asm = new AttributeSetManager();
		new AttributeValueManager();
		pavdm = new ProductAttributeDropdownValueManager();
	}
	public String execute() 
	{
		this.setProduct(pm.getProductByUuid(this.getProductUuid()));
		Iterator<AttributeSet> attrsItr = pasm.getAssignedAttributeSet(this.getProduct()).iterator();
		Map <AttributeSet,List<ProductAv>> localattributeSetToAv = new HashMap<AttributeSet,List<ProductAv>>();
		while(attrsItr.hasNext())
		{
			AttributeSet localAttrS = attrsItr.next();
			localattributeSetToAv.put(localAttrS, pasm.getProductAvByAttributeSetAndProduct(this.getProduct(),localAttrS));
		}
		this.setAttributeSetToAv(localattributeSetToAv);
		this.setAttributesets(pasm.getUnAssignedAttributeSet(this.getProduct()));
		return SUCCESS ;
	}
	public String updateProductAttribute()
	{
		Product p = pm.getProductByUuid(this.getProductUuid());
		AttributeSet as = asm.getAttributeSetByUuid(this.getAttributeSetUuId());
		Iterator<ProductAv> pavLstItr = pasm.getProductAvByAttributeSetAndProduct(p, as).iterator();
		while(pavLstItr.hasNext())
		{
			ProductAv pav = pavLstItr.next();
			pav.setAttributeValues(null);
			Attribute a = pav.getAttribute();
			Set<AttributeValue> newAvsSet = new HashSet<AttributeValue>();
			if(a.isMultivalued())
			{
				Iterator<AttributeValue> valItr = a.getValues().iterator();
				while(valItr.hasNext())
				{
					AttributeValue av = valItr.next();
					if(av.getAttributeType().equals("checkbox"))
					{
						String k = "chk_"+av.getUuid() ;
						if(this.request.getParameter(k.trim()) != null)
						{
							newAvsSet.add(av);
						}
					}
					else if(av.getAttributeType().equals("dropdown")||av.getAttributeType().equals("radio"))
					{
						ProductAttributeDropdownValue padv = pavdm.getProductAttributeDropdownValueByProductAndAttributeValue(p, av);
						if(padv == null)
						{
							padv = new ProductAttributeDropdownValue();
							padv.setProduct(p);
							padv.setAttributeValue(av);
						}
						String k = "drop_"+av.getUuid() ;
						String value = (String) this.request.getParameter(k.trim());
						padv.setSelected(value);
						pavdm.save(padv);
					}
				}
				pav.setAttributeValues(newAvsSet);
				pasm.save(pav);
			}
			else
			{
				String k = "txt_"+pav.getUuid() ;
				String value = (String) this.request.getParameter(k.trim());
				pav.setValue(value);
				pasm.save(pav);
			}
		}
		this.setProduct(p);
		Iterator<AttributeSet> attrsItr = pasm.getAssignedAttributeSet(this.getProduct()).iterator();
		Map <AttributeSet,List<ProductAv>> localattributeSetToAv = new HashMap<AttributeSet,List<ProductAv>>();
		while(attrsItr.hasNext())
		{
			AttributeSet localAttrS = attrsItr.next();
			localattributeSetToAv.put(localAttrS, pasm.getProductAvByAttributeSetAndProduct(p,localAttrS));
		}
		this.setAttributeSetToAv(localattributeSetToAv);
		this.setAttributesets(pasm.getUnAssignedAttributeSet(this.getProduct()));
		return SUCCESS ;
	}
	public String assignAttributeSet()
	{
		AttributeSet as = asm.getAttributeSetByUuid(this.getAttributeSetUuId());
		Product p = pm.getProductByUuid(this.getProductUuid());
		this.setProduct(p);
		Iterator<Attribute> attrItr = as.getAttributes().iterator();
		while(attrItr.hasNext())
		{
			Attribute a = attrItr.next();
			pasm.assignAttributeSet(p, as, a);
		}
		Iterator<AttributeSet> attrsItr = pasm.getAssignedAttributeSet(this.getProduct()).iterator();
		Map <AttributeSet,List<ProductAv>> localattributeSetToAv = new HashMap<AttributeSet,List<ProductAv>>();
		while(attrsItr.hasNext())
		{
			AttributeSet localAttrS = attrsItr.next();
			localattributeSetToAv.put(localAttrS, pasm.getProductAvByAttributeSetAndProduct(p,localAttrS));
		}
		this.setAttributeSetToAv(localattributeSetToAv);
		this.setAttributesets(pasm.getUnAssignedAttributeSet(this.getProduct()));
		return SUCCESS ;
	}
	public String unassignAttributeSet()
	{
		Product p = pm.getProductByUuid(this.getProductUuid());
		AttributeSet as = asm.getAttributeSetByUuid(this.getAttributeSetUuId());
		pasm.unassignAttributeSet(p,as);
		this.setProduct(p);
		Iterator<AttributeSet> attrsItr = pasm.getAssignedAttributeSet(this.getProduct()).iterator();
		Map <AttributeSet,List<ProductAv>> localattributeSetToAv = new HashMap<AttributeSet,List<ProductAv>>();
		while(attrsItr.hasNext())
		{
			AttributeSet localAttrS = attrsItr.next();
			localattributeSetToAv.put(localAttrS, pasm.getProductAvByAttributeSetAndProduct(p,localAttrS));
		}
		this.setAttributeSetToAv(localattributeSetToAv);
		this.setAttributesets(pasm.getUnAssignedAttributeSet(this.getProduct()));
		return SUCCESS ;
	}
	
	public Long getProductUuid() {
		return productUuid;
	}
	public void setProductUuid(Long productUuid) {
		this.productUuid = productUuid;
	}
	public List<AttributeSet> getAttributesets() {
		return attributesets;
	}
	public void setAttributesets(List<AttributeSet> attributesets) {
		this.attributesets = attributesets;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public List<AttributeSet> getAssignedAttributesets() {
		return assignedAttributesets;
	}
	public void setAssignedAttributesets(List<AttributeSet> assignedAttributesets) {
		this.assignedAttributesets = assignedAttributesets;
	}
	public int getP() {
		return p;
	}
	public void setP(int p) {
		this.p = p;
	}
	public Long getAttributeSetUuId() {
		return attributeSetUuId;
	}
	public void setAttributeSetUuId(Long attributeSetUuId) {
		this.attributeSetUuId = attributeSetUuId;
	}
	public Map<AttributeSet, List<ProductAv>> getAttributeSetToAv() {
		return attributeSetToAv;
	}
	public void setAttributeSetToAv(Map<AttributeSet, List<ProductAv>> attributeSetToAv) {
		this.attributeSetToAv = attributeSetToAv;
	}
	public boolean ifChecked(Set<AttributeValue> avs,AttributeValue av)
	{
		Iterator<AttributeValue> avItr = avs.iterator();
		while(avItr.hasNext())
		{
			AttributeValue localAv = avItr.next();
			if(localAv.getUuid().equals(av.getUuid()))
			{
				return true ;
			}
		}
		return false ;
	}
	public List<String> getList(String valueStr)
	{
		List<String> values = new ArrayList<String>();
		StringTokenizer sttoken = new StringTokenizer(valueStr,"#");
		while(sttoken.hasMoreTokens())
		{
			String token = sttoken.nextToken().trim();
			values.add(token);
		}
		return values ;
	}
	public String getSelected(Product p, AttributeValue av)
	{
		String selected = null ;
		ProductAttributeDropdownValue padv = pavdm.getProductAttributeDropdownValueByProductAndAttributeValue(p, av);
		if(padv != null)
		{
			selected = padv.getSelected();
		}
		return selected;
		
	}
	@Override
	public void setServletRequest(HttpServletRequest req) {
		// TODO Auto-generated method stub
		this.request = req;
	}
}
